package com.zzx.schoolfellow.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.vdurmont.emoji.EmojiParser;
import com.zzx.schoolfellow.common.api.ApiResult;
import com.zzx.schoolfellow.jwt.JwtUtil;
import com.zzx.schoolfellow.mapper.SensitiveWordMapper;
import com.zzx.schoolfellow.model.dto.CreateTopicDTO;
import com.zzx.schoolfellow.model.entity.BmsPost;
import com.zzx.schoolfellow.model.entity.SensitiveWord;
import com.zzx.schoolfellow.model.entity.UmsUser;
import com.zzx.schoolfellow.model.vo.PostVO;
import com.zzx.schoolfellow.service.IBmsPostService;
import com.zzx.schoolfellow.service.IUmsUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.*;

/**
 * @program: schoolfeoolw_backend
 * @description: 帖子控制层
 * @author: zzx
 * @create: 2023-03-14 21:57
 **/
@RestController
@RequestMapping("/post")
public class BmsPostController extends BaseController {

    @Resource
    private IBmsPostService iBmsPostService;
    @Resource
    private IUmsUserService umsUserService;

    @GetMapping("/list")
    public ApiResult<Page<PostVO>> list(@RequestParam(value = "tab", defaultValue = "latest") String tab,
                                        @RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo,
                                        @RequestParam(value = "status", defaultValue = "1") String status,
                                        @RequestParam(value = "size", defaultValue = "10") Integer pageSize) {
        Page<PostVO> list = iBmsPostService.getList(new Page<>(pageNo, pageSize), tab, status);
        return ApiResult.success(list);
    }

    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public ApiResult<BmsPost> create(@RequestHeader(value = JwtUtil.USER_NAME) String userName
            , @RequestBody CreateTopicDTO dto) {
        UmsUser user = umsUserService.getUserByUsername(userName);
        BmsPost topic = iBmsPostService.create(dto, user);
        return ApiResult.success(topic);
    }
    @GetMapping()
    public ApiResult<Map<String, Object>> view(@RequestParam("id") String id) {
        Map<String, Object> map = iBmsPostService.viewTopic(id);
        return ApiResult.success(map);
    }

    @GetMapping("/recommend")
    public ApiResult<List<BmsPost>> getRecommend(@RequestParam("topicId") String id) {
        List<BmsPost> topics = iBmsPostService.getRecommend(id);
        return ApiResult.success(topics);
    }

    @PostMapping("/update")
    public ApiResult<BmsPost> update(@RequestHeader(value = JwtUtil.USER_NAME) String userName, @Valid @RequestBody BmsPost post) {
        UmsUser umsUser = umsUserService.getUserByUsername(userName);
        Assert.isTrue(umsUser.getId().equals(post.getUserId()), "非本人无权修改");
        post.setModifyTime(new Date());
        post.setContent(EmojiParser.parseToAliases(post.getContent()));
        iBmsPostService.updateById(post);
        return ApiResult.success(post);
    }

    @DeleteMapping("/delete/{id}")
    public ApiResult<String> delete(@RequestHeader(value = JwtUtil.USER_NAME) String userName, @PathVariable("id") String id) {
        UmsUser umsUser = umsUserService.getUserByUsername(userName);
        BmsPost byId = iBmsPostService.getById(id);
        Assert.notNull(byId, "来晚一步，话题已不存在");
        Assert.isTrue(byId.getUserId().equals(umsUser.getId()), "你为什么可以删除别人的话题？？？");
        iBmsPostService.removeById(id);
        return ApiResult.success(null, "删除成功");
    }
//    @DeleteMapping("/likeadd/{id}")
//    public ApiResult<String> likeadd(@RequestHeader(value = JwtUtil.USER_NAME) String userName, @PathVariable("id") String id) {
//        UmsUser umsUser = umsUserService.getUserByUsername(userName);
//        BmsPost byId = iBmsPostService.getById(id);
//
//        iBmsPostService.removeById(id);
//        return ApiResult.success(null,"删除成功");
//    }
//    @DeleteMapping("/unlikeadd/{id}")
//    public ApiResult<String> unlikeadd(@RequestHeader(value = JwtUtil.USER_NAME) String userName, @PathVariable("id") String id) {
//        UmsUser umsUser = umsUserService.getUserByUsername(userName);
//        BmsPost byId = iBmsPostService.getById(id);
//        Assert.notNull(byId, "来晚一步，话题已不存在");
//        Assert.isTrue(byId.getUserId().equals(umsUser.getId()), "你为什么可以删除别人的话题？？？");
//        iBmsPostService.removeById(id);
//        return ApiResult.success(null,"删除成功");
//    }

    @PostMapping("/approve")
    public void approvePost(@Valid @RequestBody BmsPost po) {
        BmsPost post = iBmsPostService.getById(po.getId());
        post.setStatus("1");
        iBmsPostService.updateById(post);
    }

    @PostMapping("/reject")
    public void rejectPost(@RequestBody Long id) {
        BmsPost post = iBmsPostService.getById(id);
        if (post != null) {
            post.setStatus("rejected");
            iBmsPostService.updateById(post);
        }
    }

    @GetMapping("/lists")
    public List<BmsPost> getPostList(@RequestParam("keyword") String keyword) {
        List<BmsPost> list;
        if (Objects.equals(keyword, "")) {
            list = iBmsPostService.list();
        } else {
            list = iBmsPostService.list(new QueryWrapper<BmsPost>()
                    .like("title", keyword));
        }
        return list;
    }

    @DeleteMapping("/remove")
    public void deletePost(@RequestParam("id") String id) {
        iBmsPostService.removeById(id);
    }

}